package com.lhy.utilsdemo.demo;

import java.util.concurrent.CompletableFuture;

/**
 * @author Liu Hangyu
 * @date 2021/12/15 15:25
 */
public class CompleteFutureDemo {

    public static void main(String[] args) throws Exception {
        //1
        CompletableFuture<String> firstFuture = CompletableFuture.supplyAsync(() -> {
            System.out.println("firstFuture");
            return "firstFuture";
        });

        //2
        CompletableFuture<String> secondFuture = CompletableFuture.supplyAsync(() -> {
            System.out.println("沉睡5秒");
            try {
                Thread.sleep(5000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return "secondFuture";
        });
        //全部完成后向下执行
        CompletableFuture.allOf(firstFuture, secondFuture).join();
        //任何一个完成后向下执行
        //CompletableFuture.anyOf(bookFuture, tableFuture).join();

        System.out.println("firstFuture -> " + firstFuture.get());
        System.out.println("secondFuture -> " + secondFuture.get());
    }

}
